package com.chenbei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.system.domain.SysConfig;
import com.chenbei.system.enums.SysConfigStatusEnum;
import com.chenbei.system.mapper.SysConfigMapper;
import com.chenbei.system.service.ISysConfigService;
import com.chenbei.system.vo.req.SysConfigReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class SysConfigService extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Override
    public void add(SysConfig config) {
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfig::getName, config.getName()).or().eq(SysConfig::getKeyName, config.getKeyName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("配置名称或键名已存在");
        }
        save(config);
    }

    @Override
    public void update(SysConfig config) {
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(SysConfig::getId, config.getId()).and(consumer -> {
            consumer.eq(SysConfig::getName, config.getName()).or().eq(SysConfig::getKeyName, config.getKeyName());
        });
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("配置名称或键名已存在");
        }
        updateById(config);
    }

    @Override
    public void deleteById(long id) {
        SysConfig config = getById(id);
        if (Objects.equals(config.getStatus(), SysConfigStatusEnum.YES.getStatus())) {
            throw new LogicHandleException("内置配置不能删除");
        }
        removeById(id);
    }

    @Override
    public Page<SysConfig> listPage(SysConfigReq sysConfigReq) {
        Page<SysConfig> pageConfig = new Page<>(sysConfigReq.getPageNum(), sysConfigReq.getPageSize());
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysConfig::getId);
        if (StringUtils.isNoneBlank(sysConfigReq.getName())) {
            lambdaQueryWrapper.like(SysConfig::getName, sysConfigReq.getName());
        }
        if (StringUtils.isNoneBlank(sysConfigReq.getKeyName())) {
            lambdaQueryWrapper.like(SysConfig::getKeyName, sysConfigReq.getKeyName());
        }
        if (sysConfigReq.getStatus() != null && sysConfigReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysConfig::getStatus, sysConfigReq.getStatus());
        }
        return page(pageConfig, lambdaQueryWrapper);
    }

    @Override
    public SysConfig getByKey(String key) {
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfig::getKeyName, key).last("LIMIT 1");
        return getOne(lambdaQueryWrapper);
    }
}
